package com.aring.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.web.multipart.MultipartFile;

public class FileProcessor {
	
	/**
	 * 保存文件，返回文件名
	 * @param path  文件保存路径
	 * @param file  上传的文件
	 * @return  文件名
	 */
	public static String save(String path,MultipartFile file) throws Exception{
			String name = getNewName(file.getOriginalFilename());
			FileOutputStream fos = null;
			InputStream is = file.getInputStream();
			byte[] buffer = new byte[1024];
			fos = new FileOutputStream(new File(path+name));
			while ((is.read(buffer))!=-1) {
				fos.write(buffer);
			}
			fos.flush();
		    fos.close();
		return name;
	}
	
	
	
	
	
	private static String getNewName(String oldName){
        String extendName = oldName.substring(oldName.lastIndexOf(".")+1);
		DateFormat format = new SimpleDateFormat("yyyymmddhhmmssSSS");
		return format.format(new Date())+"."+extendName;
	}
	
	
	
}
